package com.heima.homeservicemanagementsystem.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.homeservicemanagementsystem.entity.ImageVo;
import com.heima.homeservicemanagementsystem.entity.ServiceClassification;
import com.heima.homeservicemanagementsystem.entity.dto.Option;
import com.heima.homeservicemanagementsystem.utils.FileUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import com.heima.homeservicemanagementsystem.module.PageParams;
import com.heima.homeservicemanagementsystem.module.Result;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.*;
import com.heima.homeservicemanagementsystem.entity.ReachableImmediately;
import com.heima.homeservicemanagementsystem.service.ReachableImmediatelyService;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import static cn.dev33.satoken.SaManager.log;

/**
 * 即可达;(reachable_immediately)表控制层
 *
 * @author : http://www.chiner.pro
 * @date : 2025-4-12
 */
@Tag(name = "即可达对象功能接口")
@RestController
@RequestMapping("/reachableImmediately")
public class ReachableImmediatelyController {
    @Autowired
    private Environment environment;
    @Resource
    private ReachableImmediatelyService reachableImmediatelyService;

    @Operation(summary = "分页查询")
    @PostMapping("/page")
    public Result<Page<ReachableImmediately>> paginQuery(@RequestBody PageParams<ReachableImmediately> pageParams) {
        //查询参数
        ReachableImmediately params = pageParams.getParams();
        LambdaQueryWrapper<ReachableImmediately> lqw = new LambdaQueryWrapper<>();


        lqw.eq(params.getType() != null, ReachableImmediately::getType, params.getType());


        lqw.eq(params.getFlName() != null, ReachableImmediately::getFlName, params.getFlName());


        lqw.eq(params.getFlImage() != null, ReachableImmediately::getFlImage, params.getFlImage());


        lqw.eq(params.getParentId() != null, ReachableImmediately::getParentId, params.getParentId());


        return Result.success(reachableImmediatelyService.page(pageParams.getPage(), lqw));
    }
    @Operation(summary = "即可达父级下拉列表")
    @GetMapping("/serviceParent")
    public Result<List<Option>> roleOptions() {
        //查询参数

        QueryWrapper<ReachableImmediately> lqw = new QueryWrapper<>();
        lqw.select("fl_name AS label", "id AS value");
        lqw.eq("type", 0);
        List<Map<String, Object>> maps = reachableImmediatelyService.listMaps(lqw);

        List<Option> optionals = BeanUtil.copyToList(maps, Option.class);
        return Result.success(optionals);
    }

    @Operation(summary = "即可达所有搜索")
    @GetMapping("/list")
    public Result searchQuery() {
        return Result.success(reachableImmediatelyService.list());
    }
    @Operation(summary = "手机端获取")
    @GetMapping("/listbylimt")
    public Result searchQuerylimit() {
        QueryWrapper<ReachableImmediately> wrapper = new QueryWrapper<>();
        wrapper.last("LIMIT 5");
        return Result.success(reachableImmediatelyService.list(wrapper));
    }

    @PostMapping("/upload")
    @Operation(summary = "后端分类图片上传")
    public Result<ArrayList<ImageVo>> upload(@RequestParam("file") MultipartFile file) {
        log.info("文件:{}", file);
        String port = environment.getProperty("server.port");
//        System.out.println("Server port: " + port);
        String upload = FileUtils.upload(file);

        ImageVo imageVo = new ImageVo();
        String randomId = UUID.randomUUID().toString();
        imageVo.setUid(randomId);
        imageVo.setName(upload);
        imageVo.setStatus("done");
        imageVo.setUrl("http://127.0.0.1:" + port + "/upload/" + upload);
        ArrayList<ImageVo> objects = new ArrayList<>();
        objects.add(imageVo);

        return Result.success("文件上传成功！", objects);


    }

    @Operation(summary = "有条件的搜索")
    @PostMapping("/tjSearch")
    public Result Query(@RequestBody ReachableImmediately reachableImmediately) {
        LambdaQueryWrapper<ReachableImmediately> lqw = new LambdaQueryWrapper<>();


        lqw.eq(reachableImmediately.getId() != null, ReachableImmediately::getId, reachableImmediately.getId());


        lqw.eq(reachableImmediately.getType() != null, ReachableImmediately::getType, reachableImmediately.getType());


        lqw.like(reachableImmediately.getFlName() != null, ReachableImmediately::getFlName, reachableImmediately.getFlName());


//        lqw.eq(reachableImmediately.getFlImage()!=null,ReachableImmediately::getFlImage,reachableImmediately.getFlImage());


        lqw.eq(reachableImmediately.getParentId() != null, ReachableImmediately::getParentId, reachableImmediately.getParentId());


        return Result.success(reachableImmediatelyService.list(lqw));
    }

    /**
     * 新增数据
     *
     * @param reachableImmediately 实例对象
     * @return 实例对象
     */
    @Operation(summary = "新增数据")
    @PostMapping("/insert")
    public Result<Boolean> insert(@RequestBody ReachableImmediately reachableImmediately) {
        if (reachableImmediatelyService.save(reachableImmediately)) {
            return Result.success();
        }
        return Result.error();
    }

    /**
     * 更新数据
     *
     * @param reachableImmediately 实例对象
     * @return 实例对象
     */
    @Operation(summary = "更新数据")
    @PutMapping("update")
    public Result<Boolean> update(@RequestBody ReachableImmediately reachableImmediately) {
        if (reachableImmediatelyService.updateById(reachableImmediately)) {
            return Result.success();
        }
        return Result.error();
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Operation(summary = "通过主键删除数据",
            parameters = {
                    @Parameter(name = "id", description = "主键", required = true, in = ParameterIn.PATH)
            })
    @DeleteMapping("/delete/{id}")
    public Result<Boolean> deleteById(@PathVariable String id) {

        if (reachableImmediatelyService.removeById(id)) {
            return Result.success();
        }
        return Result.error();
    }

    @Operation(summary = "多id删除数据")
    @DeleteMapping("/delete")
    public Result<Boolean> deleteByIds(@RequestBody List<String> id) {
        log.info("前端信息：{}", id);
        if (reachableImmediatelyService.removeByIds(id)) {
            return Result.success();
        }
        return Result.error();
    }
}